package com.goat.train.common.intercptor;

import com.goat.train.common.exception.BusinessException;
import com.goat.train.common.respond.CommonResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public CommonResp handleException(Exception e) {
        log.error("系统异常：", e);
        return CommonResp.fail("系统异常，请联系管理员");
    }

    @ExceptionHandler(BusinessException.class)
    public CommonResp handleException(BusinessException e) {
        log.error("业务异常：", e);
        return CommonResp.fail(e.getBusinessExceptionEnum().getMessage());
    }

    @ExceptionHandler(BindException.class)
    public CommonResp handleException(BindException e) {
        log.error("校验异常：{}{}", e, e.getAllErrors().get(0).getDefaultMessage());
        return CommonResp.fail(e.getAllErrors().get(0).getDefaultMessage());
    }
}
